package cn.vin.utils;


import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.checkerframework.checker.nullness.Opt;

import java.util.*;
import java.util.concurrent.*;

public class GuavaUtils {
    public static void main(String[] args) {
        LoadingCache<String, String> cache = CacheBuilder.newBuilder()
                .maximumSize(1000)
                .expireAfterWrite(3, TimeUnit.SECONDS)
                .build(new CacheLoader<String, String>() {
                    @Override
                    public String load(String key) throws Exception {
                        System.out.println("查询缓存");
                        return null;
                    }
                });

        try {
            System.out.println(cache.get(""));
        } catch (Exception e) {
            System.out.println("出错");
        }
    }

    public static void fun() {
        LoadingCache<String, Optional<String>> cache = CacheBuilder.newBuilder()
                .maximumSize(1000)
                .expireAfterWrite(3, TimeUnit.SECONDS)
                .build(new CacheLoader<String, Optional<String>>() {
                    @Override
                    public Optional<String> load(String key) throws Exception {
                        System.out.println("查询缓存");
                        return Optional.ofNullable(null);
                    }
                });
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        while(true) {
            Scanner sc = new Scanner(System.in);
            sc.nextLine();
            threadPool.submit(() -> {
                Optional<String> test = null;
                try {
                    test = cache.get("test");
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                System.out.println("查询结果" + test.orElse(null));
                System.out.println("======================");
            });

        }
    }
}
